package Master;

import java.net.InetAddress;

import Master.interfaces.InputScreen;

/*
 * Heart of the master program
 */
public class MasterEngine {
    /*

      This is an coordinator of master.
      Step 1. receive the tasks list from ClientGUI.

      Step 2. add the tasks into a queue

      Step 3. distribute the tasks according to the status of workers

      Step 4. get the status info from worker and send back to display in ClientGUI

     */	
	private InputScreen client_gui;
    //private final Timer timer;
    
    public static enum AlgorithmFlag {FCFS, COST, TIME};
    public static AlgorithmFlag algorithmFlag = AlgorithmFlag.FCFS;
    private boolean isFirstTaskPlan=true;

    /*
    public MasterEngine(){
        timer = new Timer();
    }
    */
    
    public void launchGUI()
    {
    	System.out.println("Launching the GUI...");
    	client_gui = new InputScreen();
    	client_gui.startup(this);
    }

    public boolean execute(TaskPlan taskPlan){
    	ScheduleAlgorithm scheduleAlgorithm = new ScheduleAlgorithm(taskPlan);    	
    	
    	System.out.println("algorithmFlag.ordinal() =" + 
    												algorithmFlag.ordinal());

    	boolean isFeasible = true;
    	switch(algorithmFlag.ordinal()){
    	case 0:
    		isFeasible = scheduleAlgorithm.fcfsBasedSchedule();
    		break;
    	case 1:
    		isFeasible = scheduleAlgorithm.timeBasedSchedule();
    		break;
    	case 2:
    		isFeasible = scheduleAlgorithm.timeBasedBudgetConstrainedSchedule();
    		break;
    	}
    
    	// began to run the dispatcher
    	if(this.isFirstTaskPlan)
    	{
    		for(Resource resource : taskPlan.getResources())
                resource.runDispatcher();

    		this.isFirstTaskPlan=false;
        }
    	return isFeasible;
    }
    
    /*
     * VOID MAIN
     */
    public static void main(String[] args) {
    	//Custom security manager, turned off for DEMO purpose
    	//System.setProperty("java.security.policy", "local.policy");
        //System.setSecurityManager(new SecurityManager());
    	
        try {
            // get the information about the local host address
            InetAddress address = InetAddress.getLocalHost();
            String hostAddress = address.getHostAddress();
            String hostname = address.getHostName();

            if(args.length == 1) {
                hostAddress = args[0];
            } else if (args.length != 0) {
                System.exit(-1);
            }

            MasterEngine masterEngine = new MasterEngine();
            masterEngine.launchGUI();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /*
    //No longer needed anymore
    private class CountDownTask extends TimerTask {
        int seconds;

        public CountDownTask(int seconds){
            this.seconds = seconds;
        }
        public void run(){
            System.out.println("remaining time : " + seconds);
            if(seconds == 0) {
                timer.cancel();
            }
            seconds--;
        }
    }

    public void cancelTimer(){
        this.timer.cancel();
    }

    public void timeStart(int seconds){
        TimerTask tt = new CountDownTask(seconds);
        timer.schedule(tt, 0, 1000);
    }
    */
}
